package com.jiang.demo.depotstable.dds.datasource;

import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;

import java.util.HashMap;
import java.util.Map;

/**
 * @author jiangzhe
 * @create 2021/5/11 14:22
 */
public class DDS extends AbstractRoutingDataSource {

    private static DDS instance;
    private static byte[] lock = new byte[0];
    private static Map<Object, Object> dsMap = new HashMap<>();

    @Override
    public void setTargetDataSources(Map<Object, Object> targetDataSources) {
        super.setTargetDataSources(targetDataSources);
        dsMap.putAll(targetDataSources);
        super.afterPropertiesSet();
    }

    public Map<Object, Object> getDataSourceMap(){
        return dsMap;
    }

    public static synchronized DDS getInstance(){
        if(null == instance){
            synchronized (lock){
                if(null == instance){
                    instance = new DDS();
                }
            }
        }
        return instance;
    }

    @Override
    protected Object determineCurrentLookupKey() {
        return DSContextHolder.getDBType();
    }
}
